package com.wl.dzt.sys.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 字符串工具
 */
public class StringUtil {

    /**
     * 空字符串
     * 判断某个字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean emptyStr(String str) {
        return str == null || str.equals("");
    }

    /**
     * 空字符串
     * 判断某一批字符串是否为空 其中一个为空则都为空
     *
     * @param strs
     * @return
     */
    public static boolean emptyStrs(String... strs) {
        boolean isTrue = true;
        for (String str : strs) {
            isTrue = emptyStr(str);
            if (!isTrue)
                break;
        }
        return isTrue;
    }

    /**
     * 首字母小写
     *
     * @param str
     * @return
     */
    public static String fristLowerCase(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1, str.length());
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String fristUpperCase(String str) {
        return str.substring(0, 1).toUpperCase() + str.substring(1, str.length());
    }

    /**
     * 生成uuid
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 截取字符串
     * 截取字符串用.分割后的最后一位,再替换.为任意字符
     *
     * @param src
     * @param sign
     * @return
     */
    public static String getStringFilterLast(String src, String sign) {
        String ary[] = src.split("[.]");
        if (ary.length == 1) {
            return null;
        }
        List<String> list = new ArrayList<>();
        for (int i = 0; i < ary.length - 1; i++) {
            list.add(ary[i]);
        }
        return CollectionUtil.join(list, sign);
    }

    /**
     * 字符串格式化
     *
     * @param text
     * @param data
     * @return String
     */
    public static String fmt(String text, Map<String, Object> data) {
        Pattern p = Pattern.compile("(\\{[^{}]*\\})+");
        Matcher m = p.matcher(text);
        while (m.find()) {
            String rex = m.group(1);
            String key = rex.replace("{", "").replace("}", "");
            Object val = data.get(key);
            if (val == null) {
                throw new RuntimeException("（" + text + "）存在异常,检查表达式和所指向是否正确!");
            } else {
                text = text.replace(rex, val.toString());
            }
        }
        return text;
    }

    /**
     * 获取字符串大括号内的值
     *
     * @param text
     * @return
     */
    public static String getFmtStr(String text) {
        Pattern p = Pattern.compile("(\\{[^{}]*\\})+");
        Matcher m = p.matcher(text);
        while (m.find()) {
            String rex = m.group(1);
            String key = rex.replace("{", "").replace("}", "");
            return key;
        }
        return null;
    }

    /**
     * 首字母大写
     *
     * @param srcStr
     * @return
     */
    public static String firstCharacterToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }

    /**
     * 替换分隔符并转换为大写
     *
     * @param srcStr
     * @param org
     * @param ob
     * @return
     */
    public static String replaceUnderlineAndfirstToUpper(String srcStr, String org, String ob) {
        String newString = "";
        int first = 0;
        while (srcStr.indexOf(org) != -1) {
            first = srcStr.indexOf(org);
            if (first != srcStr.length()) {
                newString = newString + srcStr.substring(0, first) + ob;
                srcStr = srcStr.substring(first + org.length(), srcStr.length());
                srcStr = firstCharacterToUpper(srcStr);
            }
        }
        newString = newString + srcStr;
        return newString;
    }

    /**
     * 驼峰命名
     *
     * @param str
     * @return
     */
    public static String hump(String str) {
        return replaceUnderlineAndfirstToUpper(str, "_", "");
    }

}
